package com.huiztech.maystall.activity;

import com.huiztech.maystall.photo.CropImageActivity;
import com.huiztech.maystall.photo.ModifyAvatarDialog;

import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.text.Layout;
import android.text.Spannable;
import android.text.SpannableStringBuilder;
import android.text.TextUtils;
import android.text.style.AbsoluteSizeSpan;
import android.text.style.AlignmentSpan;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.Toast;

public class MyInfoActivity extends Activity {

    private String TAG = "MyInfoActivity";
    private static final int FLAG_CHOOSE_IMG = 5;
    private static final int FLAG_CHOOSE_PHONE = 6;
    private static final int FLAG_MODIFY_FINISH = 7;
    private String path = "";

    private RelativeLayout userSafetyLayout, notpayLayout, msgLayout, backProductLayout, mylikeLayout;
    private ImageButton msgBtn, takePhotoBtn;
    private ImageView myIconImageView;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_myinfo);

        myIconImageView = (ImageView) findViewById(R.id.myinfo_icon_image);
        backProductLayout = (RelativeLayout) findViewById(R.id.myinfo_back_product_layout);
        takePhotoBtn = (ImageButton) findViewById(R.id.myinfo_takephoto_btn);
        msgLayout = (RelativeLayout) findViewById(R.id.myinfo_msg_center_layout);
        mylikeLayout = (RelativeLayout) findViewById(R.id.myinfo_mylike_layout);
        userSafetyLayout = (RelativeLayout) findViewById(R.id.myinfo_safety_layout);
        notpayLayout = (RelativeLayout) findViewById(R.id.myinfo_notpay_layout);

        // 返回
        backProductLayout.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                Intent intent = new Intent(MyInfoActivity.this, BackProductActivity.class);
                startActivity(intent);
            }
        });
        // 照相
        takePhotoBtn.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                photoDialog();
            }
        });
        // 信息
        msgLayout.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                Intent intent = new Intent(MyInfoActivity.this, MsgCenterActivity.class);
                startActivity(intent);
            }
        });
        // 我喜欢的商品
        mylikeLayout.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                intentProductList("I like");
            }
        });
        // 用户安全
        userSafetyLayout.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                Intent intent = new Intent(MyInfoActivity.this, UserSafetyActivity.class);
                startActivity(intent);
            }
        });
        // 未支付
        notpayLayout.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                Intent intent = new Intent(MyInfoActivity.this, ShoppingCartActivity.class);
                startActivity(intent);
            }
        });

    }

    private void intentProductList(String title) {
        Intent intent = new Intent(MyInfoActivity.this, ProductListActivity.class);
        intent.putExtra("title", title);
        startActivity(intent);
    }

    private void photoDialog() {
        // 调用选择那种方式的dialog
        ModifyAvatarDialog modifyAvatarDialog = new ModifyAvatarDialog(this) {
            // 选择本地相册
            @Override
            public void doGoToImg() {
                this.dismiss();
                Intent intent = new Intent();
                intent.setAction(Intent.ACTION_PICK);
                intent.setType("image/*");
                startActivityForResult(intent, FLAG_CHOOSE_IMG);
            }

            // 选择相机拍照
            @Override
            public void doGoToPhone() {
                this.dismiss();
                Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
                MyInfoActivity.this.startActivityForResult(intent, 1);
            }
        };
        // 不知道干啥的 ===============================
        AlignmentSpan span = new AlignmentSpan.Standard(Layout.Alignment.ALIGN_CENTER);
        AbsoluteSizeSpan span_size = new AbsoluteSizeSpan(25, true);
        SpannableStringBuilder spannable = new SpannableStringBuilder();
        String dTitle = getString(R.string.gl_choose_title);
        spannable.append(dTitle);
        spannable.setSpan(span, 0, dTitle.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
        spannable.setSpan(span_size, 0, dTitle.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
        modifyAvatarDialog.setTitle(spannable);
        modifyAvatarDialog.show();
        // 不知道干啥的 ===============================

    }

    // 选择照片后返回activity
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (requestCode == FLAG_CHOOSE_IMG && resultCode == RESULT_OK) {
            if (data != null) {
                Uri uri = data.getData();
                if (!TextUtils.isEmpty(uri.getAuthority())) {
                    Cursor cursor = getContentResolver().query(uri, new String[] { MediaStore.Images.Media.DATA },
                            null, null, null);
                    if (null == cursor) {
                        Toast.makeText(MyInfoActivity.this, "图片没找到", 0).show();
                        return;
                    }
                    cursor.moveToFirst();
                    String path = cursor.getString(cursor.getColumnIndex(MediaStore.Images.Media.DATA));
                    cursor.close();
                    Log.i(TAG, "path=" + path);
                    Intent intent = new Intent(this, CropImageActivity.class);
                    intent.putExtra("path", path);
                    startActivityForResult(intent, FLAG_MODIFY_FINISH);
                } else {
                    Log.i(TAG, "path=" + uri.getPath());
                    Intent intent = new Intent(this, CropImageActivity.class);
                    intent.putExtra("path", uri.getPath());
                    startActivityForResult(intent, FLAG_MODIFY_FINISH);
                }
            }
        } else if (requestCode == 1 && resultCode == RESULT_OK) {

            Bundle bundle = data.getExtras();
            if (bundle != null) {
                Bitmap bm = (Bitmap) bundle.get("data");
                myIconImageView.setImageBitmap(bm);
            }

        } else if (requestCode == FLAG_MODIFY_FINISH && resultCode == RESULT_OK) {
            if (data != null) {
                path = data.getStringExtra("path");
                Log.i(TAG, "截取到的图片路径是 = " + path);

                Bitmap b = BitmapFactory.decodeFile(path);
                myIconImageView.setImageBitmap(b);

            }
        }
    }
}
